﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ITSTOREBLL;

namespace ITStore.Controllers
{
    public class DonHangController : Controller
    {
        private ITStoreEntities db = new ITStoreEntities();
        //
        // GET: /DonHang/

        public ActionResult Index()
        {
            DonHangBLL gdhbll = new DonHangBLL();
            gdhbll.TaoDonHang(this.HttpContext);
            if (gdhbll.TaoDonHang(this.HttpContext).ToString() == "00000000-0000-0000-0000-000000000000")
            {
                return RedirectToAction("Index", "Home");
            }
            var ctdh = from c in db.ChiTietDonHangs.Include("SanPham")
                       where c.ctdh_DonHang == gdhbll.MaDonHang
                       select c;
            return View(ctdh.ToList());
        }
        public ActionResult ThanhToanThanhCong()
        {
            return View();
        }

        public ActionResult Them(int id)
        {
            DonHangBLL dhbll = new DonHangBLL();
            //dhbll.TaoDonHang(this.HttpContext);
            if (dhbll.TaoDonHang(this.HttpContext).ToString() == "00000000-0000-0000-0000-000000000000")
            {
                return RedirectToAction("Index", "Home");
            }
            dhbll.ThemDonHang(id);

            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult ThanhToan()
        {
            DonHangBLL dhbll = new DonHangBLL();
            dhbll.ThanhToan(this.HttpContext);
            this.HttpContext.Session.Clear();
            return RedirectToAction("ThanhToanThanhCong");
        }

        public ActionResult XoaToanBoDonHang()
        {
            DonHangBLL dhbll = new DonHangBLL();
            dhbll.XoaTatCaDonHang(this.HttpContext);
            return RedirectToAction("Index");
        }

        public ActionResult XoaMotSanPham(int masanpham)
        {
            DonHangBLL dhbll = new DonHangBLL();
            dhbll.XoaMotSanPham(this.HttpContext,masanpham);
            return RedirectToAction("Index");
        }
    }
}
